package org.nobject.common.image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;


/**
 * 绘图工具类
 *
 * @author bianrongjun
 * @version 1.0
 */
public class DrawUtils {
	
	/** 
	 * draw
	 * @param context
	 * @param bgImage
	 * @param fontColor
	 * @param font
	 * @param destPath
	 * @param width
	 * @param height
	 * @throws Exception
	 */
	public static void draw(String context,BufferedImage bgImage,Color fontColor,Font font,String destPath,int width,int height) throws Exception{
		boolean empty=false;
		
		//指定长宽
		if (bgImage== null) {
			empty=true;
			bgImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		} else {
			try {
				bgImage=ImageUtils.subImgInScale(bgImage, width, height, ImageUtils.SUBIMG_TYPE_CENTER);
				//image=image.getSubimage(0, 0, width, height);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		Graphics2D g = bgImage.createGraphics();
		g.setFont(font);
		g.setColor(fontColor);
		if(empty){
			g.setBackground(Color.white);
			g.clearRect(0, 0, width, height);
		}
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.drawString(context,0,40);
		g.dispose();
		bgImage.flush();
		File outputFile=new File(destPath);
		if(!outputFile.exists()){
			if(!outputFile.getParentFile().exists()) outputFile.getParentFile().mkdirs();
			outputFile.createNewFile();
		} 
		//Graphics g = image.getGraphics();
		
		ImageIO.write(bgImage, "JPEG", outputFile);
		
	}
}
